//
//  XMHomeHeaderView.m
//  XunMi
//
//  Created by MacBook on 2024/2/18.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMHomeHeaderView.h"

@interface XMHomeHeaderView() <SDCycleScrollViewDelegate>
@property (nonatomic, strong) NSMutableArray *urlArray;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, assign) CGRect imageViewFrame;
@end


@implementation XMHomeHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

#pragma mark - setter

- (void)setDataArray:(NSArray<XMBannerModel *> *)dataArray {
    _dataArray = dataArray;
    [self.urlArray removeAllObjects];
    __block NSMutableArray *imgUrlArray = @[].mutableCopy;
    [dataArray enumerateObjectsUsingBlock:^(XMBannerModel *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
        [imgUrlArray addObject:[NSString xm_stringNoNullWith:obj.cover.file_url]];
        [self.urlArray addObject:[NSString xm_stringNoNullWith:obj.link]];
    }];
    self.bannerView.imageURLStringsGroup = imgUrlArray;
}

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    XMBannerModel *model = _dataArray[index];
    if (self.webBlock) {
        self.webBlock(model);
    }
}

#pragma mark - setupSubviews

- (void)setupSubviews {
    self.backgroundColor = kHEXA(@"FFFFFF", 0);
    [self addSubview:self.bannerView];

    [self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (SDCycleScrollView *)bannerView {
    if (_bannerView == nil) {
        _bannerView = [[SDCycleScrollView alloc] initWithFrame:CGRectZero];
        _bannerView.delegate = self;
        _bannerView.showPageControl = NO;
        _bannerView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
        _bannerView.backgroundColor = kHEXA(@"FFFFFF", 0);
        _bannerView.pageDotColor = UIColor.whiteColor;
        _bannerView.currentPageDotColor = kAppMainColor;
        _bannerView.autoScrollTimeInterval = 5.0;
        _bannerView.pageControlDotSize = CGSizeMake(12, 12);
        _bannerView.placeholderImage = kGetImage(@"banner_place");
    }
    return _bannerView;
}

- (NSMutableArray *)urlArray {
    if (_urlArray == nil) {
        _urlArray = [NSMutableArray array];
    }
    return _urlArray;
}

- (void)scrollViewDidScroll:(CGFloat)contentOffsetY {
    CGRect frame = self.imageViewFrame;
    frame.size.height -= contentOffsetY;
    frame.origin.y = contentOffsetY;
    self.imageView.frame = frame;
}

@end
